﻿@using SubstrateHelper.Models;
@using SubstrateHelper.Data;
@using ProcessBase.Models;
@using System.Collections.Concurrent;
@using System.Collections.Generic;
@using System.Linq;
@using System.Timers;
@using BootstrapBlazor.Components;


@inject SubstrateHelperDbContext Db
@inject MessageService MessageService

@inject AuthenticationStateProvider AuthenticationStateProvider
@inject UserManager<IdentityUser> UserManager
@inject NavigationManager NavigationManager
<div>
    <div class="m-1">
        @*<span>
            <strong class="text-success">主批号 </strong>：
            <select @bind="SelectedMainLotNo">
                <option value="-1">请选择主批号</option>
                @if (MainLotNos != null)
                {
                    @for (int i = 0; i < MainLotNos.Count; i++)
                    {
                        <option value="@i">@MainLotNos[i]</option>
                    }
                }

            </select>
        </span>*@

        <span >
            <strong class="text-success">工序 </strong>：
            <select @bind="SelectedProcess">
                <option value="-1">所有类型</option>
                @if (Processes != null)
                {
                    @for (int i = 0; i < Processes.Count; i++)
                    {
                        <option value="@i">@Processes[i]</option>
                    }
                }

            </select>
        </span>









        <Button class="ml-5" IsOutline="false" Color="Color.Success" @onclick="NewRecord">新增记录</Button>

        <Button class="ml-5" IsOutline="false" Color="Color.Primary" @onclick="SaveRecord">保存更改</Button>

    </div>



    <hr class="bg-primary" style="height:5px;" />

    <ComponentProcessDetail ProcessDetails="ProcessDetails" @ref="componentProcessDetail" />


</div>
<Message />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
@code {

    ComponentProcessDetail componentProcessDetail { get; set; }


    List<string> MainLotNos
    {
        get
        {
            if (_MainLotNos == null)
            {
                _MainLotNos = Db.Orders.Select(x => x.vbatchcode).Distinct().OrderBy(x => x).ToList();
            }
            return _MainLotNos;
        }
    }
    List<string> _MainLotNos = null;
    int SelectedMainLotNo { get; set; } = -1;




    List<string> Processes
    {
        get
        {
            if (_Processes == null)
            {
                try
                {
                    _Processes = Db.Parameters.Where(x=>x.ProcessName!="00#领料").Select(x => x.ProcessName).Distinct().OrderBy(x => x).ToList();
                }
                catch
                {
                    _Processes = null;
                }
            }
            return _Processes;
        }


    }
    List<string> _Processes = null;



    List<SelectedItem> QualityStates
    {
        get
        {
            if (_QualityStates == null)
            {
                _QualityStates = new List<SelectedItem>();
                _QualityStates.Add(new SelectedItem(null, "未处理"));

                foreach (var x in Db.CandidateLists.Where(x => x.Category == "质量状态").Select(x => x.Candidate))
                {
                    var item = new SelectedItem(x, x);
                    _QualityStates.Add(item);
                }
            }
            return _QualityStates;
        }
        set
        {
            _QualityStates = value;
        }
    }
    List<SelectedItem> _QualityStates = null;

    List<SelectedItem> InputLots { get; set; }

    int SelectedProcess
    {
        get
        {
            return _SelectedProcess;
        }
        set
        {
            _SelectedProcess = value;
        }
    }
    int _SelectedProcess = -1;



    string LotNo { get; set; }

    List<ProcessBase.Models.ProcessDetail> ProcessDetails
    {
        get
        {
            return _ProcessDetails;
        }
        set
        {
            _ProcessDetails = value;

        }
    }
    List<ProcessBase.Models.ProcessDetail> _ProcessDetails = null;




    bool _IsEditor()
    {

        return Helpers.UserHelper.HasRole(user, "录入员");




    }
    private void NewRecord()
    {

        if (_IsEditor())
        {
            @*//主批号
            if (SelectedMainLotNo < 0)
            {
                MessageService?.Show(new MessageOption()
                {
                    Content = $"发生错误：主批号不能为空。",
                    Icon = "fa fa-info-circle",
                    ShowDismiss = true,
                    Color = Color.Danger
                });
                return;
            }*@

            if (SelectedProcess < 0)
            {
                MessageService?.Show(new MessageOption()
                {
                    Content = $"发生错误：工序不能为空。",
                    Icon = "fa fa-info-circle",
                    ShowDismiss = true,
                    Color = Color.Danger
                });
                return;
            }



            //ProcessDetails = Helpers.ProcessDetailHelper.NewDetails(MainLotNos[SelectedMainLotNo], Processes[SelectedProcess]);
            ProcessDetails = Helpers.ProcessDetailHelper.NewDetails(Processes[SelectedProcess]);

        }
        else
        {
            MessageService?.Show(new MessageOption()
            {
                Content = $"发生错误：你登录的角色没有修改数据的权限。",
                Icon = "fa fa-info-circle",
                ShowDismiss = true,
                Color = Color.Danger
            });
        }

    }

    private void _UpdateDetail(string guid)
    {




        //
        ProcessDetails = Helpers.ProcessDetailHelper.GetDetails(guid);

        //



    }







    private void SaveRecord()
    {
        componentProcessDetail.SaveRecord();


    }





    string user { get; set; }
    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var User = authState.User;

        user = UserManager.GetUserId(User);

    }




}

